package org.hawk.gwt.ppc.examples.arrays;

import java.lang.reflect.Array;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
/**
 * This example represents arrays creation through reflection.
 * 
 * @author alex.bereznevatiy@gmail.com
 */
public class Arrays implements EntryPoint {

	public void onModuleLoad() {
		
		Object array = Array.newInstance(DummyObject.class, 4);
		
		DummyObject[] arrayCasted = (DummyObject[])array;
		
		RootPanel.get().add(new HTML("Newly created: " + toString(arrayCasted)));
		for (int i = 0; i < 4; i++) {
			Array.set(array, i, new DummyObject());
		}
		RootPanel.get().add(new HTML("Modified: " + toString(arrayCasted)));
	}

	private String toString(Object array) {
		StringBuilder result = new StringBuilder();
		result.append(array.getClass().getName());
		result.append(" [");
		for (int i = 0; i < Array.getLength(array); i++) {
			if (i > 0) {
				result.append(", ");
			}
			result.append(Array.get(array, i));
		}
		return result.append(']').toString();
	};

}
